<template>
  <div class="login">
    <div class="wrap">
      <div class="img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
// import { login } from "../http/user.js";
export default {
  data() {
    return {
      ruleForm: { username: "", password: "" },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
        this.$store.dispatch("user/loginFn", this.ruleForm).then(()=>{
          this.$router.push('/')
        },()=>{
          this.resetForm(formName)
        })
        } else {
          this.resetForm(formName)
          return false;
        }
      });
    },
    // async loginFn() {
    //   let { username, password } = this.ruleForm;
    //   let res = await login({ username, password });
    //   console.log(res);
    //   if (res.meta.status == 200) {
    //     localStorage.setItem("tokenLogin", res.data.token);
    //     this.$router.push("/");
    //     this.$message({
    //       message: res.meta.msg,
    //       type: "success",
    //     });
    //   } else {
    //     this.$message.error(res.meta.msg);
    //   }
    // },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  background-color: #2b4b6b;
  padding-bottom: 50px;
  .wrap {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 500px;
    background-color: white;
    height: 260px;
    margin: auto;
    padding: 80px;
    box-sizing: border-box;
    box-shadow: 0 0 10px #ddd;
    .img {
      width: 100px;
      height: 100px;
      line-height: 100px;
      position: absolute;
      top: -40px;
      left: 230px;
      border-radius: 50%;
      background-color: white;
      text-align: center;
      margin-bottom: 30px;
      padding: 10px;
      box-sizing: border-box;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #efefef;
      }
    }
  }
}
</style>